/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.site.managedbeans;

import br.com.fatecpg.songs.dao.ArtistDAO;
import br.com.fatecpg.songs.dao.util.DAOException;
import br.com.fatecpg.songs.dao.util.DAOFactory;
import br.com.fatecpg.songs.entity.Artist;
import br.com.fatecpg.songs.util.FacesContextUtil;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;


/**
 *
 * @author Marcos
 */
@ManagedBean (name = "artistSearch")
@RequestScoped
public class ArtistManagedBean {

    private Artist artist;
    private final ArtistDAO artistDAO;

    public ArtistManagedBean() throws DAOException {
        artistDAO = (ArtistDAO) DAOFactory.getDAO(Artist.class);
        artist = getArtistSearch();
    }

    public List<Artist> getResult() {
        String search = FacesContextUtil.getParameter("search");
        if (search != null) {
            return artistDAO.search(search);
        } else {
            return new ArrayList<Artist>();
        }
    }

    private Long getId() {
        if (FacesContextUtil.getParameter("idArtist") != null) {
            Long id = Long.parseLong(FacesContextUtil.getParameter("idArtist"));
            return id;
        }
        return null;
    }

    private Artist getArtistSearch() {
        Long id = getId();
        if (id != null) {            
            artist = artistDAO.load(id);
            return artist;
        }
        return null;
    }

    public Artist getArtist() {
        return artist;
    }

    public void setArtist(Artist artist) {
        this.artist = artist;
    }

}
